package com.sniff.atp.service.order;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.ClientLoginDTO;
import com.sniff.atp.enums.api.OrderAPI;
import com.sniff.atp.utils.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Set;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.service.order
 * @Author: zxy
 * @CreateTime: 2022-10-31 11:37
 * @Description: 创建订单
 * @Version: 1.0
 */
@Service
public class CreateOrdinaryOrderService {
    private ClientLoginDTO clientLoginDTO;

    public void setLogin(ClientLoginDTO clientLoginDTO){
        this.clientLoginDTO = clientLoginDTO;
    }


    /**
     * Client：根据商品链接查询商品
     */
    public String goodsUrl(String url) {
        JSONObject param = new JSONObject();
        param.put("detailUrl", url);

        return new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doPostJson(OrderAPI.GOODS_URL.getApi(), param);
    }

    /**
     * Client: 添加购物车
     */
    public String addCart(String productSku, int orderQuantity){
        JSONObject param = JsonUtil.readJson("order/CommonProduct");
        param.replace("addCartSource", clientLoginDTO.getSystemSource());
        param.getJSONArray("commonProductItemList").getJSONObject(0).replace("productSku", productSku);
        param.getJSONArray("commonProductItemList").getJSONObject(0).replace("orderQuantity", orderQuantity);

        String result = new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doPostJson(OrderAPI.ADD_CART.getApi(), param);
        return result;
    }

    /**
     * Client: 查询购物车列表
     */
    public String cartList(){
        String result = new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doGetURL(OrderAPI.CART_LIST.getApi());
        return result;
    }

    /**
     * Client: 创建订单
     * @param cartIdList
     * @param orderType 订单类型：1 直发，2 囤货
     * @return
     */
    public String createOrder(Object cartIdList, Integer orderType){
        String systemSource = clientLoginDTO.getSystemSource();

        // 判断如果是2B 订单类型改为2
        if (systemSource.equals("2")) {
            orderType = 2;
        }
        String customerOrderNo = "ZFTest" + RandomUtil.numberString(6);
        // 判断是否为囤货订单
        if (orderType == 2){
            customerOrderNo = "THTest" + RandomUtil.numberString(6);
        }
        JSONObject param = JsonUtil.readJson("order/createOrder");
        param.replace("cartIdList", cartIdList);
        param.replace("customerOrderNo", customerOrderNo);
        param.replace("orderType", orderType);
        param.replace("useOneTheWayInventory", ToolsUtil.getStringBuild("getUseOneTheWayInventory"));

        String result = new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doPostJson(OrderAPI.CREATE_ORDER.getApi(), param);
        return result;
    }

    /**
     * Client: 清空购物车
     */
    public String deleteCart(ClientLoginDTO clientLoginDTO){
        this.clientLoginDTO = clientLoginDTO;
        String s2 = cartList();
        ArrayList<String> cartIdList = new ArrayList<>();
        JSONObject cartDatas = JSON.parseObject(s2);
        JSONArray data = cartDatas.getJSONArray("data");
        for (Object cartData: data) {
            JSONArray cartList = ((JSONObject) cartData).getJSONArray("cartList");
            for (Object cartIds: cartList) {
                String cartId = ((JSONObject) cartIds).getString("cartId");
                cartIdList.add(cartId);
            }
        }

        JSONObject param = new JSONObject();
        param.put("cartIdList", cartIdList);

        if(cartIdList.isEmpty()){
            return "购物车无其它商品！！";
        }

        String result = new HttpClientUtil(Headers.setClientHeader(clientLoginDTO)).doPostJson(OrderAPI.DELETE_CART.getApi(), param);
        return result;
    }

    /**
     * Client: 创建单品订单流程
     */
    public String createOrderProcess(ClientLoginDTO clientLoginDTO, String productUrl, int orderNum, int orderType){

        setLogin(clientLoginDTO);

        String s = goodsUrl(productUrl);
        LogUtil.warn("【订单流程】根据商品链接查询商品：" + productUrl);
        String productSku = JsonUtil.extractString(s, "data.skuList[0].productSku");
        addCart(productSku, orderNum);
        LogUtil.warn("【订单流程】添加购物车，productSku：" + productSku);

        String s2 = cartList();
        ArrayList<String> cartIdList = new ArrayList<>();
        JSONObject cartDatas = JSON.parseObject(s2);
        JSONArray data = cartDatas.getJSONArray("data");
        for (Object cartData: data) {
            JSONArray cartList = ((JSONObject) cartData).getJSONArray("cartList");
            for (Object cartIds: cartList) {
                String cartId = ((JSONObject) cartIds).getString("cartId");
                cartIdList.add(cartId);
            }
        }
        LogUtil.warn("【订单流程】购物车列表查询！！");
        String order = createOrder(cartIdList, orderType);
        LogUtil.warn("【订单流程】创建订单成功，返回数据：" + order);

        return order;
    }

    /**
     * CLient: 创建多品订单
     */
    public String createOrderProcessM(ClientLoginDTO clientLoginDTO, JSONObject productInfo, int orderType){

        setLogin(clientLoginDTO);

        Set<String> productSkus = productInfo.keySet();
        for (String productUrl: productSkus) {
            Integer orderNum = (Integer) productInfo.get(productUrl);

            String s = goodsUrl(productUrl);
            LogUtil.warn("【订单流程】根据商品链接查询商品：" + productUrl);
            String productSku = JsonUtil.extractString(s, "data.skuList[0].productSku");
            addCart(productSku, orderNum);
            LogUtil.warn("【订单流程】添加购物车，productSku：" + productSku);
        }

        String s2 = cartList();
        ArrayList<String> cartIdList = new ArrayList<>();
        JSONObject cartDatas = JSON.parseObject(s2);
        JSONArray data = cartDatas.getJSONArray("data");
        for (Object cartData: data) {
            JSONArray cartList = ((JSONObject) cartData).getJSONArray("cartList");
            for (Object cartIds: cartList) {
                String cartId = ((JSONObject) cartIds).getString("cartId");
                cartIdList.add(cartId);
            }
        }
        LogUtil.warn("【订单流程】购物车列表查询！！");
        String order = createOrder(cartIdList, orderType);
        LogUtil.warn("【订单流程】创建订单成功，返回数据：" + order);
        return order;
    }


}
